#include "AutoSplitter.h"
#include <QtCore/QRect>
#include <QtGui/QDesktopWidget>
#include <QtGui/QApplication>
#include "AutoSplitterHandle.h"

AutoSplitter::AutoSplitter(QWidget* parent)
:QSplitter(desktopDirection() ,parent)
{
	this->setHandleWidth(4);
	connect (QApplication::desktop(),SIGNAL(workAreaResized(int)),this,SLOT(refreshLayout()));
}

AutoSplitter::~AutoSplitter(void)
{
}

Qt::Orientation AutoSplitter::desktopDirection() const{
	const QRect rect=QApplication::desktop()->screenGeometry();
	return rect.width()>rect.height()?Qt::Horizontal:Qt::Vertical;
}

void AutoSplitter::refreshLayout(){
	setOrientation(desktopDirection());
}

QSplitterHandle * AutoSplitter::createHandle (){
	return new AutoSplitterHandle(this->orientation(),this);
}

void AutoSplitter::onResizeArea(){
	AutoSplitterHandle* handle=qobject_cast<AutoSplitterHandle*>(this->handle(0));
	if(handle){
		handle->resizeHandleWidth();
	}
}
